package com.imooc.luckymoney.controller;

import com.imooc.luckymoney.configuration.LuckyMoneyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * the annotation @RestController is equal to @Controller + @ResponseBody
 *
 */
@RestController
public class HelloController {
    /**
     * how to use annotation @GetMapping
     * @return
     */
    @GetMapping(value = "/hello")
    public String sayHello(){
        return "first SpringBoot demo";
    }

    /**
     * how to use annotation @RequestMapping
     * @return
     */
    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public String sayHello2(){
        return "second SpringBoot demo";
    }

    /**
     * 单个属性的注入
     * 先在配置文件中写好，然后使用@Value注解导入
     */
    @Value("${minMoney}")
    private BigDecimal minMoney;
    @GetMapping(value = "/singleProperty")
    public String printSingleProperty(){
        return "minMoney: " + minMoney;
    }


    /**
     * 多属性注入
     * 1。先在配置文件中配置好
     * 2。建立一个配置类LuckyMoneyProperties
     * 3。使用@Autowired注入
     */
    @Autowired
    private LuckyMoneyProperties luckyMoneyProperties;
    @GetMapping(value = "/getLuckyMoneyProperties")
    public String getLuckyMoneyProperties(){
        return luckyMoneyProperties.getDescription();
    }

    /**
     * 获取URL中的参数 @PathVariable
     * @param id
     * @return
     */
    @GetMapping(value = "/getId/{id}")
    public String getRequestParam(@PathVariable Integer id){
        return "id: "+ id;
    }

    /**
     * 获取URL中的参数 @RequestParam
     * 形式：?id=123
     * @param id
     * @return
     */
    @GetMapping(value = "/getId")
    public String getRequestParam2(@RequestParam(value = "id") Integer id){
        return "id: "+ id;
    }

}
